/**
 * 项目名称：MSH
 * 包名称：com.mt.common.sys.service
 * 文件名称：RoleServiceImpl.java
 */
package com.mt.system.serviceImpl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.flex.remoting.RemotingInclude;
import org.springframework.stereotype.Service;

import com.mt.component.exception.DataOperateException;
import com.mt.system.dao.RoleDao;
import com.mt.system.domain.Role;
import com.mt.system.domain.User;
import com.mt.system.service.RoleService;


/**
 * 类名称：RoleServiceImpl <br>
 * 类描述： <br>
 * 创建人： 马腾 <br>
 * 创建时间：Oct 18, 2012 1:03:57 PM <br>
 */
@Service("RoleService")
@RemotingDestination(channels = {"my-amf"})
public class RoleServiceImpl implements RoleService {
	
	@Resource
	private RoleDao roleDao;

	@RemotingInclude
	@Override
	public Role addRole(Role role) {
		role.setOperateTime(new Date());
		if(roleDao.getRoleByName(role.getName()) != null){
			throw new DataOperateException(DataOperateException.FIND_REPATE_DATA , "add role failure , find repate role , name is " + role.getName());
		}
		return roleDao.addRole(role);
	}

	@RemotingInclude
	@Override
	public void updateRole(Role role) {
		role.setOperateTime(new Date());
		roleDao.updateRole(role);
	}

	@RemotingInclude
	@Override
	public void deleteRole(Role role) {
		Role temp = roleDao.getRoleByName(role.getName());
		if(temp != null){
			if(roleDao.isBindUser(temp)){
				throw new DataOperateException(DataOperateException.FIND_USER_BIND_ROLE , "delete role failure , find user to binding the role");
			}else{
				roleDao.deleteRole(temp);
			}
		}
	}

	@Override
	@RemotingInclude
	public List<Role> getAllRoles() {
		return roleDao.getAllRoles();
	}

	@RemotingInclude
	@Override
	public List<Role> getRolesByUser(User user) {
		return roleDao.getRolesByUser(user);
	}

	@Override
	public Role getRoleByName(String name) {
		return roleDao.getRoleByName(name);
	}

	@RemotingInclude
	@Override
	public void updateRolePermission(Role role) {
		Role temp = roleDao.getRoleByName(role.getName());
		if(temp != null){
			temp.setPermissions(role.getPermissions());
			roleDao.updateRole(temp);
		}
	}
}
